import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Index from '../views/index.vue'
import Users from '../views/uesrs/users.vue'
import Rights from '../views/power/rights.vue'
import Roles from '../views/power/roles.vue'
import Goods from '../views/product/goods.vue'
import Categories from '../views/product/categories.vue'
import GoodsAdd from '../views/product/goods-add.vue'

Vue.use(VueRouter)

const routes = [{
  path: '/',
  redirect: '/index'
},
{
  path: '/login',
  component: Login
},
{
  path: '/index',
  component: Index,
  children: [
    { path: '/users', component: Users },
    { path: '/rights', component: Rights },
    { path: '/roles', component: Roles },
    { path: '/goods', component: Goods },
    { path: '/categories', component: Categories },
    { path: '/goods-add', component: GoodsAdd }
  ]

}
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  if (to.path === '/login' || token) {
    next()
  } else {
    next('/login')
  }
})
export default router
